package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;
import jdk.nashorn.internal.scripts.JD;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserImpl implements UserDao {
    @Override
    public UserModel findByUsername(String username) {
//        数据实现层DAO
//        1.编写sql语句
        String sql = "select * from user where username='" + username + "'";

//        2.调用JDBC
        ResultSet resultSet = JDBC.find(sql);
//        判斷结果集有没有数据
        UserModel userModel = null;
        try {
            while (resultSet.next()) {
                userModel = new UserModel();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        3.返回结果
//        4.关系连接
        return userModel;
    }

    @Override
    public int add(UserModel userModel) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = new Date();
        String dateString = simpleDateFormat.format(date);

        String sql = "insert into user (username,password," +
                "sex,phone," +
                "hobby,birthday," +
                "introduce,create_time," +
                "update_time,enable) " +
                "values('" + userModel.getUsername() + "','" + userModel.getPassword() + "'," +
                "'" + userModel.getSex() + "','" + userModel.getPhone() + "'," +
                "'" + userModel.getHobby() + "','" + userModel.getBirthday() + "'," +
                "'" + userModel.getIntroduce() + "','" + dateString + "'," +
                "'" + dateString + "',0)";

        return JDBC.update(sql);
    }

    @Override
    public UserModel findByUsernameAndPassword(UserModel userModel) {
        UserModel model = null;
        String sql = "select * from user where username='" + userModel.getUsername() + "' and password='" + userModel.getPassword() + "'";
        ResultSet resultSet = JDBC.find(sql);
        try {
            while (resultSet.next()) {
                model = new UserModel();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public List<UserModel> findAll() {
//        封装sql

        String sql = "select * from user";
        ResultSet resultSet = JDBC.find(sql);
        List<UserModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                UserModel userModel = getModel(resultSet);
                list.add(userModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<UserModel> findAll(UserModel model) {
        String sql = "select * from user where 1=1";
        if (model.getUsername() != null && !"".equals(model.getUsername())) {
            sql += " and username like '%" + model.getUsername() + "%'";
        }
        if (model.getSex() != null && !"全部".equals(model.getSex())) {
            sql += " and sex = '" + model.getSex() + "'";
        }
        if (model.getPhone() != null && !"".equals(model.getPhone())) {
            sql += " and phone like '%" + model.getPhone() + "%'";
        }
//        生日
        if (!"".equals(model.getStartBirthday()) && "".equals(model.getEndBirthday())) {
            sql += " and birthday >='" + model.getStartBirthday() + "'";
        }
        if ("".equals(model.getStartBirthday()) && !"".equals(model.getEndBirthday())) {
            sql += " and birthday <='" + model.getEndBirthday() + "'";
        }
        if (model.getStartBirthday() != null && !"".equals(model.getStartBirthday()) && model.getEndBirthday() != null && !"".equals(model.getEndBirthday())) {
            sql += " and birthday between  '" + model.getStartBirthday() + "' and '" + model.getEndBirthday() + "'";
        }
//愛好

        if (model.getHobby() != null && !"".equals(model.getHobby())) {
            String[] hobbys = model.getHobby().split(",");
            for (int i = 0; i < hobbys.length; i++) {
                if (i == 0)
                    sql += " and  find_in_set('" + hobbys[i] + "',hobby) ";
                else
                    sql += " or  find_in_set('" + hobbys[i] + "',hobby) ";
            }
        }
        if (model.getEnable() != null && model.getEnable() != -1) {
            sql += " and  enable =" + model.getEnable();
        }

        if (model.getIntroduce() != null && !"".equals(model.getIntroduce())) {
            sql += " and introduce like '%" + model.getIntroduce() + "%'";
        }
        ResultSet resultSet = JDBC.find(sql);
        List<UserModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                UserModel userModel = getModel(resultSet);
                list.add(userModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 模糊查询
     * 1.在html页面里面写入查询条件的代码
     * 2.在js里面  findAll 里面添加查询语句的data
     * 3.在servlet里面增加取值，封装model
     * 4.注意：不要直接修改以前的接口代码，因为有可能别人再用，
     * 5.需要重新写一个接口，重载关系
     * 6.sql语句的拼接  难点，数组，和日期
     * 7.自己建立数据库的时候每一个表都应该有，创建时间，更新时间，状态，备注，主键id
     *
     *
     */
    @Override
    public UserModel findById(int id) {
        String sql = "select * from user where id=" + id;
        ResultSet resultSet = JDBC.find(sql);
        UserModel userModel = null;
        try {
            while (resultSet.next()) {
                userModel = getModel(resultSet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userModel;
    }

    @Override
    public int update(UserModel userModel) {
        String sql = "update user set phone='" + userModel.getPhone() + "'," +
                "sex='" + userModel.getSex() + "'," +
                "introduce='" + userModel.getIntroduce() + "'," +
                "birthday='" + userModel.getBirthday() + "'," +
                "hobby='" + userModel.getHobby() + "' where id=" + userModel.getId();
//        if (userModel.getPhone() != "" || userModel.getPhone() != null) {
//            sql += " phone='" + userModel.getPhone() + "'";
//        }
//        if (userModel.getSex() != "" || userModel.getSex() != null) {
//            sql += " sex='" + userModel.getSex() + "'";
//        }
//        if (userModel.getIntroduce() != "" || userModel.getIntroduce() != null) {
//            sql += " introduce='" + userModel.getIntroduce() + "'";
//        }
//        if (userModel.getBirthday() != "" || userModel.getBirthday() != null) {
//            sql += " birthday='" + userModel.getBirthday() + "'";
//        }
//        if (userModel.getHobby() != "" || userModel.getHobby() != null) {
//            sql += " hobby='" + userModel.getHobby() + "'";
//        }
//        sql += "where id=" + userModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public UserModel judgePassword(UserModel userModel) {
        String sql = "select * from user where id=" + userModel.getId() + " and password='" + userModel.getOldPassword() + "'";
        UserModel model = null;
        ResultSet resultSet = JDBC.find(sql);
        try {
            while (resultSet.next()) {
                model = getModel(resultSet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int del(int id) {
        String sql = "delete from user where id=" + id;
        return JDBC.update(sql);
    }

    /**
     * 提取公共部分
     *
     * @param resultSet
     * @return
     */
    private UserModel getModel(ResultSet resultSet) {
        UserModel userModel = new UserModel();
        try {
            userModel.setId(resultSet.getInt("id"));
            userModel.setUsername(resultSet.getString("username"));
            userModel.setPhone(resultSet.getString("phone"));
            userModel.setEmail(resultSet.getString("email"));
            userModel.setSex(resultSet.getString("sex"));
            userModel.setBirthday(resultSet.getString("birthday"));
            userModel.setHobby(resultSet.getString("hobby"));
            userModel.setIntroduce(resultSet.getString("introduce"));
            userModel.setCreateTime(resultSet.getString("create_time"));
            userModel.setEnable(resultSet.getInt("enable"));
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return userModel;
    }
}
